<?php

namespace App\Http\Controllers\Notice;

use App\Helpers\Code;
use App\Models\Notice\NoticeGroup;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class NoticeGroupController extends Controller
{

    public function __construct()
    {
        $this->_validations();

        $this->model = new NoticeGroup();
    }

    /**
     * 添加通知组
     * @param Request $request
     * @return array
     */
    public function addGroup(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addGroup($data);

            $this->checked($model, [Code::INSIDE_ERROR => 'notice.group.member_not_exist', Code::INSIDE_DATABASE_ERROR => 'common.database_error']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知组全部
     * @return array
     */
    public function getGroupListAll()
    {
        try {
            $model = $this->model->getGroupListAll();

            $this->checked($model, [Code::INSIDE_DATABASE_ERROR => 'common.database_error']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知列表
     * @param Request $request
     * @return array
     */
    public function getGroupList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int) $request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);

            $model = $this->model->getGroupList($limit, $sorter, $search);

            $this->checked($model, [Code::INSIDE_DATABASE_ERROR => 'common.database_error']);

            return preprocess($model);
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知分组详情
     * @param $id
     * @return array
     */
    public function getGroup($id)
    {
        try {
            $model = $this->model->getGroup($id);

            $this->checked($model, ['null' => 'common.recored_not_found', Code::INSIDE_DATABASE_ERROR => 'common.database_error']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑通知分组
     * @param $id
     * @param Request $request
     * @return array
     */
    public function editGroup($id, Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());
            $this->rules['title'] = ['bail', 'required', 'max:200', \Illuminate\Validation\Rule::unique('notice_groups')->ignore($id)];

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editGroup($id, $data);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => '',
                Code::INSIDE_DATABASE_ERROR => 'common.database_error'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validations()
    {
        $this->rules = [
            'title' => 'bail|required|unique:notice_groups|max:200',
            'description' => 'bail|nullable|max:200',
            'member' => 'bail|required|max:60000|array'
        ];

        $this->messages = [
            'title.required' => trans('notice.group.title_required'),
            'title.unique' => trans('notice.group.title_unique'),
            'title.max' => trans('notice.group.title_max'),
            'description.max' => trans('notice.group.description_max'),
            'member.required' => trans('notice.group.member_required'),
            'member.max' => trans('notice.group.member_max'),
            'member.array' => trans('notice.group.member_array')
        ];
    }
}
